import express, { Request, Response } from 'express';
import mysql from 'mysql';

// 创建数据库连接
const db = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: '123456',
    database: 'chuda'
});

// 连接数据库
db.connect(err => {
    if (err) {
        throw err;
    }
    console.log('MySQL Connected...');
});

const app = express();

// 创建 GET 路由
app.get('/createdb', (req: Request, res: Response) => {
    let sql = 'CREATE DATABASE nodemysql';
    db.query(sql, (err, result) => {
        if (err) throw err;
        res.send('Database created...');
        console.log(result);
    });
});

// 监听端口
app.listen(3000, () => {
    console.log('Server started on port 3000');
});